<?php

namespace System\AdminBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Base\BaseController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
 * @Route("/admin")
 */
class DefaultController extends BaseController
{

    public function __construct()
    {
//        $this->isGranted("ROLE_ADMIN");
    }


    /**
     * 后台入口
     *
     * @Route("/main/index",name="adminMainPage")
     */
    public function indexAction()
    {
        if ($this->adminChkLogin()) {
            return $this->redirectToRoute("adminHomePage");
        }
    }


    /**
     * 后台外层框架主页
     * @Route("/main/home",name="adminHomePage")
     */
    public function homeAction()
    {
        return $this->render("SystemAdminBundle:Admin:index.html.twig");
    }

    /**
     * 后台登陆处理
     * @Route("/main/logout/do",name="adminLogout")
     */
    public function logoutAction(Request $request)
    {
        $session = $request->getSession();
        $session->clear();
        return $this->redirectToRoute("adminLoginPage");
    }


    /**
     * 后台左侧按钮
     * @Route("/main/sidebar",name="adminSidebarPage")
     */
    public function sidebarAction()
    {
        return $this->render("SystemAdminBundle:Admin:sidebar.php.twig");
    }

    /**
     * 后台可用元素采集
     * @Route("/main/center",name="adminCenterPage")
     */
    public function centerAction()
    {
        $title = "模板采集汇总";
        $data = array(
            "title"   => $title,
        );
        return $this->render("SystemAdminBundle:Admin:center.html.twig",$data);
    }


    /**
     * 后台修改密码页面
     * @Route("/main/mod/password",name="modAdminPasswordPage")
     */
    public function modPasswordAction()
    {
        $title = "修改密码";
        $data = array(
            "title"   => $title,
            "submitUrl"   => $this->generateUrl("doModAdminPassword"),
            "submitType"   => "GET",
        );
        return $this->render("SystemAdminBundle:admin:modPassword.php.twig",$data);
    }

    /**
     * 后台修改密码处理
     * @Route("/main/mod/password/do",name="doModAdminPassword")
     */
    public function doModPasswordAction()
    {
        if ($oldpsw = $this->getParamRequired("oldpsw")){} else {return $this->js_out("请输入原密码",$this->generateUrl("modAdminPasswordPage"));}
        if ($newpsw = $this->getParamRequired("newpsw")){} else  {return $this->js_out("请输入新密码",$this->generateUrl("modAdminPasswordPage"));}
        if ($newpswag = $this->getParamRequired("newpswag")){} else  {return $this->js_out("请输入确认新密码",$this->generateUrl("modAdminPasswordPage"));}

        if ($newpsw != $newpswag) {
            return $this->js_out('两次输入的密码不一致，请刷新后重试！',$this->generateUrl("modAdminPasswordPage"));
        }
        $id = $this->adminChkLogin();
        $where = array(
            "id" => $id,
            "password"=>md5($oldpsw),
        );
        $DR = $this->getDoctrine()
            ->getRepository("SystemAdminBundle:Admin");
        $admin = $DR->findOneBy($where);

        if ($admin) {
            $DM = $this->getDoctrine()->getManager();
            $admin->setPassword( md5($newpsw));
            $DM->flush();
            return $this->js_out('修改成功',$this->generateUrl("modAdminPasswordPage"));
        }else{
            return $this->js_out('密码错误',$this->generateUrl("modAdminPasswordPage"));
        }
    }
}
